package com.arpa.wms.service;

import cn.hutool.core.collection.IterUtil;
import com.arpa.core.web.Result;
import com.arpa.ntocc.common.common.constant.StrConsts;
import com.arpa.ntocc.common.common.exception.ServiceException;
import com.arpa.ntocc.common.common.util.UserUtil;
import com.arpa.wms.domain.entity.Outbound;
import com.arpa.wms.domain.enums.OrderTagEnum;
import com.arpa.wms.domain.vo.OutboundVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.log4j.Log4j2;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 订单出库复核
 *
 * @author liuxr
 * @date 2021年05月19日 9:07
 */
@Service
@Log4j2
public class DeliveryReviewService {
    @Resource
    public RedissonClient redissonClient;

    @Autowired
    private IOutboundService iOutboundService;

    @Autowired
    private OutCheckService outCheckService;

    /**
     * @author liuxr
     * @date 2021/5/19 9:08
     */
   /* @Async
    //@Transactional(rollbackFor = Exception.class)
    public void batchReview(OutboundVO outboundVO) {
        String groupCode = outboundVO.getGroupCode();
        String code = outboundVO.getCode();
        RLock l = null;
        try {
            l = redissonClient.getLock(getOutBatchRecheckLockKey(code));
            if (l.tryLock()) {
                log.info("出库单复核【{}】，出库单复核获得锁成功", code);
                iOutboundService.batchRecheck(outboundVO);

                Outbound outbound = iOutboundService.getOne(new QueryWrapper<Outbound>().lambda().eq(Outbound::getCode, code).eq(Outbound::getGroupCode, UserUtil.getBranchCode()));
                if (OrderTagEnum.QUANTITY.getValue().equals(outbound.getOrderTag())) {
                    //TODO 按单拣货需要重新梳理
                    iOutboundService.outCheck(outbound);
                }else {

                    log.info("出库单复核：【step1】，处理作废单");
                    outCheckService.handleInvalid(outbound);
                    log.info("出库单复核：【step2】，调用奇门确认接口");
                    Set<String> successCode = outCheckService.deliveryConfirmation(outbound);
                    if(IterUtil.isEmpty(successCode)){
                        throw new ServiceException("出库确认失败，订单数据有误，请联系客服人员");
                    }
                    log.info("出库单复核：【step3】，更新库存");
                    outCheckService.updateInventory(successCode, outbound.getCode(),groupCode);
                    log.info("出库单复核：【step4】，更新订单信息");
                    outCheckService.updateDeliveryInfo(successCode);
                    log.info("出库单复核：【step5】，处理出库单状态");
                    outCheckService.outConfirm(outbound.getCode(), outbound.getWavePickingCode());
                    log.info("出库单复核：【step6】，异步释放容器");
                    outCheckService.releaseContainer(outbound.getWavePickingCode(),UserUtil.getBranchCode());
                }
            } else {
                log.error("出库单复核【{}】，出库单复核获得锁失败", code);
                throw new ServiceException("数据处理中，请稍后再试");
            }
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE, e);
            throw new ServiceException(e.getMessage());
        } finally {
            try {
                if (null != l && l.isLocked()) {
                    l.unlock();
                }
            } catch (Exception e) {
                log.error("释放锁失败！！", e);
            }
        }
    }*/

    /**
     * 拣货确认分布式锁key
     *
     * @param code 拣货单号
     * @return
     */
    private String getOutConfirmLockKey(String code) {
        return String.format("outConfirm-lock-%s", code);
    }

    private String getOutBatchRecheckLockKey(String code) {
        return String.format("BatchRecheck-lock-%s", code);
    }
}
